判斷出某年某月某日、是否為閏年
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input year: ");
int year = scanner.nextInt();
System.out.print("Input mouth: ");
int month = scanner.nextInt();
System.out.print("Input day: ");
int day = scanner.nextInt();
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
System.out.println(year + "是閏年");
} else {
System.out.println(year + "是平年");
}
int result = 0;
for(int i = 0; i<month; i++){
result += days[i];
}
System.out.println("第"+(result+day) + "天");
}
}
閏年的規則為以下: 非4的倍數是平年、4的倍數但非100的倍數是閏年、100的倍數但非400的倍數是平年、400的倍數是閏年,最後判斷是當年的第幾天
以下是執行後回答的結果